﻿using System;
using System.Linq;
using System.Threading.Tasks;
using HengTong.Model.Db;
using HengTong.Model.Db.Hr;
using HengTong.Model.Db.Hr.Bill;
using Microsoft.EntityFrameworkCore;
using TiaoTaoHelper.Wtm.Models;

namespace HengTong.ViewModel.Hr.SysHrInterviewRequestBillVms;

public partial class SysHrInterviewRequestBillBatchVm
{
    //批量删除
    public async Task<bool> DoBatchDeleteAsync()
    {
        if (Ids == null || !Ids.Any())
        {
            MSD.AddModelError(" ", "请选择要删除的面试申请单据");
            return false;
        }

        // 统一转换ID，避免重复定义
        var mainIds = Ids.Select(Guid.Parse).ToList();
        var nullableIds = mainIds.Select(x => (Guid?)x).ToList();

        // 检查单据状态和流程状态，只有单据状态为草稿且流程状态为待提交的才能删除
        var hasInvalidStatus = await DC.Set<SysHrInterviewRequestBill>()
            .Where(p => mainIds.Contains(p.ID))
            .AsNoTracking()
            .AnyAsync(p => p.BillStatus != BillStatus.草稿 ||
                           !p.WorkflowStatus.HasValue ||
                           p.WorkflowStatus != WorkflowStatus.待提交);

        /*if (hasInvalidStatus)
        {
            MSD.AddModelError(" ", "只有单据状态为草稿且流程状态为待提交的记录才能删除");
            return false;
        }*/

        var transaction = await DC.Database.BeginTransactionAsync();
        try
        {
            // 删除所有相关的子表数据前，先删除附件
            // 删除员工合同的附件
            var contractIds = await DC.Set<SysPersonContract>()
                .Where(e => e.PersonId.HasValue && nullableIds.Contains(e.PersonId))
                .Select(c => c.ID)
                .ToListAsync();

            if (contractIds.Any())
            {
                await DC.Set<SysPersonContractFile>()
                    .Where(a => contractIds.Contains((Guid)a.PersonContractId))
                    .ExecuteDeleteAsync();
            }

            // 删除学习经历的学历证明附件
            var educationIds = await DC.Set<SysPersonEducation>()
                .Where(e => e.PersonId.HasValue && nullableIds.Contains(e.PersonId))
                .Select(edu => edu.ID)
                .ToListAsync();

            if (educationIds.Any())
            {
                await DC.Set<SysPersonEducationFile>()
                    .Where(a => educationIds.Contains((Guid)a.PersonEducationId))
                    .ExecuteDeleteAsync();
            }

            // 删除所有相关的子表数据
            await DC.Set<SysPersonEducation>().Where(e => e.PersonId.HasValue && nullableIds.Contains(e.PersonId))
                .ExecuteDeleteAsync();
            await DC.Set<SysPersonFamily>().Where(e => e.PersonId.HasValue && nullableIds.Contains(e.PersonId))
                .ExecuteDeleteAsync();
            await DC.Set<SysPersonWork>().Where(e => e.PersonId.HasValue && nullableIds.Contains(e.PersonId))
                .ExecuteDeleteAsync();
            await DC.Set<SysPersonTrainExam>().Where(e => e.PersonId.HasValue && nullableIds.Contains(e.PersonId))
                .ExecuteDeleteAsync();
            await DC.Set<SysPersonMedical>().Where(e => e.PersonId.HasValue && nullableIds.Contains(e.PersonId))
                .ExecuteDeleteAsync();
            await DC.Set<SysPersonResume>().Where(e => e.PersonId.HasValue && nullableIds.Contains(e.PersonId))
                .ExecuteDeleteAsync();
            await DC.Set<SysPersonContract>().Where(e => e.PersonId.HasValue && nullableIds.Contains(e.PersonId))
                .ExecuteDeleteAsync();
            await DC.Set<SysPersonProfessionalTitle>()
                .Where(e => e.PersonId.HasValue && nullableIds.Contains(e.PersonId))
                .ExecuteDeleteAsync();

            // 删除主表数据
            await DC.Set<SysHrInterviewRequestBill>().Where(p => mainIds.Contains(p.ID)).ExecuteDeleteAsync();
            await DC.SaveChangesAsync();

            await transaction.CommitAsync();
            return true;
        }
        catch (Exception e)
        {
            await transaction.RollbackAsync();
            MSD.AddModelError(" ", $"批量删除异常：{e.Message}");
            return false;
        }
        finally
        {
            await transaction.DisposeAsync();
        }
    }
}